﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SGame
{
    public class SGame
    {
        #region Events
        
        #endregion
        /// <summary>
        /// between 0 and 2. 
        /// 0 = no winner
        /// 1 = player1 winner.
        /// 2 = player 2 winner.
        /// </summary>
        int winner = 0;

        private string player1, player2;
        private int index2;
        private int GameID, size;
        public static Random n = new Random();
        int startPlayer;
        private int check = 0;

        List<Ship> ShipsPlayer1;
        List<Ship> ShipsPlayer2;

        public int getGameID()
        {
            return GameID;
        }

        public string setCheck()
        {
            check++;
            if (check == 2)
            {
                if (startPlayer == 1)
                {
                    return player1;
                }
                else
                {
                    return player2;
                }
            }
            else
            {
                return "-1";
            }
        }

        public SGame(int Size, string Inviter, string Invitee, int gameID)
        {
            player1 = Inviter;
            player2 = Invitee;
            GameID = gameID;
            size = Size;
            ShipsPlayer1 = new List<Ship>();
            ShipsPlayer2 = new List<Ship>();
        }

        public int StillThere()
        {
            throw new System.NotImplementedException();
        }

        public int Surrender(string playerName)
        {
            if (playerName == player1)
            {
                winner = 2;
            }
            else
            {
                winner  = 1;
            }
            return winner;
        }

        private void first_turn_Check()
        {
            if (check == 2)
            {
                startPlayer = n.Next(1, 3);               
            }           
        }

        private void Start_Game()
        {
            throw new System.NotImplementedException();
        }

        public void Start_Battle_Phase(bool Response, string playerName)
        {
            throw new System.NotImplementedException();
        }

        public string chat(string message, string chatter)
        {
            return chatter + ": " + message;
        }

        public void set_ship(int type, int xcord, int ycord, bool horizontal, string whosShips)
        {
            Ship ship = new Ship(type, xcord, ycord, horizontal);
            if (whosShips == player1)
            {                
                this.ShipsPlayer1.Add(ship);
            }
            else if (whosShips == player2)
            {
                this.ShipsPlayer2.Add(ship);
            }
        }

        public bool checkShot(int xcord, int ycord, string player)
        {
            int xer = xcord / 16;
            int yer = ycord / 16;
            int x2, y2;
            bool hit = false;
            if (player != player1)
            {
                foreach (Ship s in ShipsPlayer1)
                {
                    if (s.rotate)
                    {
                        y2 = s.Y;
                        x2 = s.X + s.type;
                    }
                    else 
                    {
                        y2 = s.Y + s.type;
                        x2 = s.X;
                    }
                    if (s.rotate && s.Y == yer && xer >= s.X && xer < x2)
                    {
                        int temp = x2 - xer;
                        s.hits[temp - 1] = true;
                        hit = true;
                        break;
                    }
                    else if (!s.rotate && s.X == xer && yer >= s.Y && yer < y2)
                    {
                        s.hits[y2 - yer] = true;
                        hit = true;
                    }                    
                }
            }
            else if (player != player2)
            {
                foreach (Ship s in ShipsPlayer2)
                {
                    if (s.rotate)
                    {
                        y2 = s.Y;
                        x2 = s.X + s.type;
                    }
                    else
                    {
                        y2 = s.Y + s.type;
                        x2 = s.X;
                    }

                    if (s.rotate && s.Y == yer && xer >= s.X && xer < x2)
                    {
                        int temp = x2 - xer;
                        s.hits[temp - 1] = true;
                        hit = true;
                        break;
                    }
                    else if (!s.rotate && s.X == xer && yer >= s.Y && yer < y2)
                    {
                        s.hits[y2 - yer] = true;
                        hit = true;
                    }
                }
            }
            return hit;
        }
    }

    public class Ship
    {
        #region Variables

        public int type;
        public int X;
        public int Y;
        public bool rotate;
        public bool[] hits;

        #endregion
        #region Methods

        public Ship(int Typ, int x, int y, bool rot)
        {
            type = Typ;
            X = x;
            Y = y;
            rotate = rot;
            hits = new bool[Typ];
            for (int i = 0; i < Typ; i++)
            {
                hits[i] = false;
            }
        }

        #endregion
    }
}
